﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.ComponentModel;

namespace sistemaEscola
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGridView();
            }
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    object objTemp = GridView1.DataKeys[e.Row.RowIndex].Value as object;
                    if (objTemp != null)
                    {
                        string id = objTemp.ToString();

                        string codigoURL = Request["codigoMatricula"].ToString();

                        string sql1 = "";

                        sql1 = sql1 + "SELECT codigoPagamentoAluno, codigoMatricula, dataVencimentoAluno, ";
                        sql1 = sql1 + "dataPagamentoAluno, valorPagoPagamentoAluno, valorPagamentoAluno,  ";
                        sql1 = sql1 + "estadoPagamentoAluno, observacaoPagamentoAluno, tipoPagamento, ";
                        sql1 = sql1 + "(select aluno.nomeAluno from aluno, matricula where aluno.codigoAluno = matricula.codigoAluno ";
                        sql1 = sql1 + "and matricula.codigoMatricula = pagamentoAluno.codigoMatricula) as aluno ";
                        sql1 = sql1 + "FROM pagamentoAluno WHERE (codigoMatricula = " + codigoURL + ") and (codigoPagamentoAluno = " + id + ") ";

                        System.Data.SqlClient.SqlConnection conexao = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                        conexao.Open();

                        System.Data.SqlClient.SqlCommand cmdT = new System.Data.SqlClient.SqlCommand(sql1, conexao);
                        System.Data.SqlClient.SqlDataReader drT = cmdT.ExecuteReader();

                        string codigoPagamentoAluno = "";
                        string codigoMatricula = "";
                        string estadoPagamentoAluno = "";

                        int count = 0;

                        if (drT.HasRows)
                        {
                            while (drT.Read())
                            {

                                codigoPagamentoAluno = drT["codigoPagamentoAluno"].ToString();
                                codigoMatricula = drT["codigoMatricula"].ToString();
                                estadoPagamentoAluno = drT["estadoPagamentoAluno"].ToString();

                                if (estadoPagamentoAluno == "False")
                                {
                                    estadoPagamentoAluno = "Não";
                                    e.Row.Cells[7].Controls.Add(new LiteralControl(estadoPagamentoAluno));
                                }
                                else
                                {
                                    estadoPagamentoAluno = "Sim";
                                    e.Row.Cells[7].Controls.Add(new LiteralControl(estadoPagamentoAluno));
                                }

                                string validarMes = drT["dataVencimentoAluno"].ToString();

                                int posicao = validarMes.IndexOf("/");

                                validarMes = validarMes.Substring((posicao + 1), 2);

                                if (Convert.ToInt16(validarMes) <= 6)
                                {
                                    validarMes = "True";
                                }
                                else
                                {
                                    validarMes = "False";
                                }

                                if (count == 0)
                                {
                                    string editar = "<a href=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/pagamentoAlunos/Edit.aspx?codigoPagamentoAluno=" + codigoPagamentoAluno + "&codigoMatricula=" + codigoMatricula + " ><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/btEditar.gif alt=Editar /></a>";
                                    e.Row.Cells[10].Controls.Add(new LiteralControl(editar));

                                    if (drT["estadoPagamentoAluno"].ToString() == "True")
                                    {
                                        //---------------------------------Estou gerando aqui o recibo
                                        string recibo = "<a href=javascript:window.open('" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/recibo.aspx?codigoPagamentoAluno=" + codigoPagamentoAluno + "&mes=" + validarMes + "','Material','location=0,resizable=0,status=0,scrollbars=0,width=350,height=450');><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/iconeRecibo.jpg alt=Recibo width=21 height=21/></a>";
                                        e.Row.Cells[10].Controls.Add(new LiteralControl(recibo));
                                    }


                                    //string material = "<a href=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/Materials/Edit.aspx?codigoMaterial=" + codigoMaterial + " ><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/btEditar(2).gif alt=Editar /></a>";
                                    //e.Row.Cells[10].Controls.Add(new LiteralControl(material));

                                    //string excluir = "<a href=javascript:window.open('" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/deletarPagamentoAluno.aspx?codigoPagamentoAluno=" + codigoPagamentoAluno + "','Material','location=0,resizable=0,status=0,scrollbars=0,width=350,height=150');><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/btExcluirMacroProcesso.gif alt=Excluir /></a>";
                                    //e.Row.Cells[10].Controls.Add(new LiteralControl(excluir));
                                }
                                count++;
                            }
                        }
                        drT.Close();
                        cmdT.Dispose();
                        conexao.Close();
                    }
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bindGridView();
        }

        public void bindGridView()
        {
            string sql1 = "";

            string codigoURL = Request["codigoMatricula"].ToString();

            sql1 = sql1 + "SELECT codigoPagamentoAluno, codigoMatricula, dataVencimentoAluno, ";
            sql1 = sql1 + "dataPagamentoAluno, valorPagoPagamentoAluno, valorPagamentoAluno,  ";
            sql1 = sql1 + "estadoPagamentoAluno, observacaoPagamentoAluno, tipoPagamento, ";
            sql1 = sql1 + "(select aluno.nomeAluno from aluno, matricula where aluno.codigoAluno = matricula.codigoAluno ";
            sql1 = sql1 + "and matricula.codigoMatricula = pagamentoAluno.codigoMatricula) as aluno ";
            sql1 = sql1 + "FROM pagamentoAluno WHERE (codigoMatricula = " + codigoURL + ") "; //and UPPER (valorPagoPagamentoAluno) LIKE '%" + TxtNome.Text.ToUpper() + "%'  ";

            string connString = ConfigurationManager.AppSettings["baseDados"];

            SqlDataAdapter dAdapter = new SqlDataAdapter(sql1, connString);

            SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

            DataTable dTable = new DataTable();

            dAdapter.Fill(dTable);

            GridView1.DataSource = dTable;

            dTable.Columns.Add("Açoes");

            GridView1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            bindGridView();
        }
    }
}
